package com.example.mqtt.utils;

import com.alibaba.fastjson.JSONObject;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import java.text.DecimalFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class MyMqttCallback implements MqttCallback {
    DecimalFormat decimalFormat = new DecimalFormat("0.00");//保留两位小数
    String formattedNum=null;

    @Override
    public void connectionLost(Throwable throwable) {
        System.out.println("connection lost");
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) {
        String message = new String(mqttMessage.getPayload());
        System.out.println("Topic: " + s);
        System.out.println("Qos: " + mqttMessage.getQos());
//        System.out.println("Message: " + message);
        switch (s) {
            case "data1" -> {
                double data = Double.parseDouble(message);
                formattedNum=decimalFormat.format(data);
                System.out.println(formattedNum);
                JSONObject param = new JSONObject();
                param.put("data", Double.parseDouble(formattedNum));
                param.put("time", LocalTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")));
                HttpRequestUtil.sendPost("http://localhost:8080/data/1", param);
//                if (Math.abs(data - 0) > 1.5) {
//                    JSONObject webHookParam = new JSONObject();
//                    webHookParam.put("msgtype", "text");
//                    JSONObject content = new JSONObject();
//                    content.put("content", "监控报警：传感器1数据：" + data);
//                    webHookParam.put("text", content);
//                    HttpRequestUtil.sendPost("https://oapi.dingtalk.com/robot/send?access_token=edcaac5f38249d8df72a26ca80666b8534f47783443b7230a8b91ed139b2348c", webHookParam);
//                }
            }
            case "data2" -> {
                double data = Double.parseDouble(message);
                formattedNum=decimalFormat.format(data);
                System.out.println(formattedNum);
                JSONObject param = new JSONObject();
                param.put("data", Double.parseDouble(formattedNum));
                param.put("time", LocalTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")));
                HttpRequestUtil.sendPost("http://localhost:8080/data/2", param);
//                if (Math.abs(data - 10) > 1.5) {
//                    JSONObject webHookParam = new JSONObject();
//                    webHookParam.put("msgtype", "text");
//                    JSONObject content = new JSONObject();
//                    content.put("content", "监控报警：传感器2数据：" + data);
//                    webHookParam.put("text", content);
//                    HttpRequestUtil.sendPost("https://oapi.dingtalk.com/robot/send?access_token=edcaac5f38249d8df72a26ca80666b8534f47783443b7230a8b91ed139b2348c", webHookParam);
//                }
            }
            case "data3" -> {
                double data = Double.parseDouble(message);
                formattedNum=decimalFormat.format(data);
                System.out.println(formattedNum);
                JSONObject param = new JSONObject();
                param.put("data", Double.parseDouble(formattedNum));
                param.put("time", LocalTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")));
                HttpRequestUtil.sendPost("http://localhost:8080/data/3", param);
//                if (Math.abs(data - 20) > 1.5) {
//                    JSONObject webHookParam = new JSONObject();
//                    webHookParam.put("msgtype", "text");
//                    JSONObject content = new JSONObject();
//                    content.put("content", "监控报警：传感器3数据：" + data);
//                    webHookParam.put("text", content);
//                    HttpRequestUtil.sendPost("https://oapi.dingtalk.com/robot/send?access_token=edcaac5f38249d8df72a26ca80666b8534f47783443b7230a8b91ed139b2348c", webHookParam);
//                }
            }
            case "data4" -> {
                double data = Double.parseDouble(message);
                formattedNum=decimalFormat.format(data);
                System.out.println(formattedNum);
                JSONObject param = new JSONObject();
                param.put("data", Double.parseDouble(formattedNum));
                param.put("time", LocalTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")));
                HttpRequestUtil.sendPost("http://localhost:8080/data/4", param);
//                if (Math.abs(data - 30) > 1.5) {
//                    JSONObject webHookParam = new JSONObject();
//                    webHookParam.put("msgtype", "text");
//                    JSONObject content = new JSONObject();
//                    content.put("content", "监控报警：传感器4数据：" + data);
//                    webHookParam.put("text", content);
//                    HttpRequestUtil.sendPost("https://oapi.dingtalk.com/robot/send?access_token=edcaac5f38249d8df72a26ca80666b8534f47783443b7230a8b91ed139b2348c", webHookParam);
//                }
            }
        }
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        if (iMqttDeliveryToken.isComplete()) {
            System.out.println("send message successfully");
        }
    }
}
